/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pageBeans;

import controller.RegistrationControllerEJB;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Named;
import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import util.EmailSender;

/**
 *
 * @author akefeli
 */
@Named(value = "registerPageBean")
@RequestScoped
public class RegisterPageBean implements Serializable {

    @Inject
    private RegistrationControllerEJB registrationControllerEJB;
    private String firstName;
    private String lastName;
    private String email;
    private String oldPw;
    private String newPw;

    /**
     * Creates a new instance of registerPageBean
     */
    public RegisterPageBean() {
    }

    public String register() {
        registrationControllerEJB.register(email, firstName, lastName);
        String result = "An Email is sent to: " + email + " with your password";
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, result, result);

        FacesContext.getCurrentInstance().addMessage(null, msg);

        return "index";
    }

    public String changePw() {
        String result = "Email or password is wrong";
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, result, result);
        try {
            boolean pwchanged = registrationControllerEJB.changePw(email, oldPw, newPw);
            if (pwchanged) {
                result = "Your password has succesfully changed";
                msg.setDetail(result);
                msg.setSummary(result);
                msg.setSeverity(FacesMessage.SEVERITY_INFO);
            }

        } catch (Exception ex) {
            Logger.getLogger(RegisterPageBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        FacesContext.getCurrentInstance().addMessage(null, msg);
        return "changepassword";
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getNewPw() {
        return newPw;
    }

    public void setNewPw(String newPw) {
        this.newPw = newPw;
    }

    public String getOldPw() {
        return oldPw;
    }

    public void setOldPw(String oldPw) {
        this.oldPw = oldPw;
    }
}
